<?php
if (!class_exists('AlfaMVCFilter')) {
	/**
	 * @author Alecsandru Gabriel DINU
	 * @since Jun 12, 2008
	 * Alfa Controller filter. Invokes AlfaController. 
	 */
	class AlfaMVCFilter  extends AlfaFilter {
		private $devMode = false;
		
		/**
		 * Class constructor.
		 * @access public
		 * @param AlfaFilterConfig
		 */
		public function __construct(AlfaFilterConfig $filterConfig) {
			parent::__construct($filterConfig);
		}
		
		/**
		 * Performs initialization.
		 * @access public
		 */
		public function init() {
			$config = AlfaConfiguration::getInstance();
			$this->devMode = $config->getBoolean("webapp/devMode", $this->devMode);
		}

		
		/**
		 * Performs the filtering.
		 * @access public
		 * @param Context
		 * @param AlfaFilterChain
		 */
		public function doFilter(Context $context, AlfaFilterChain $chain) {
	        $controller = AlfaController::getInstance($this->devMode);
	        try {
	        	$controller->dispatch(& $context);
	        	return $chain->doFilter(& $context);
	        } catch (ResourceNotFoundException $e) {
	        	$context->redirect("/404");
	        } catch (Exception $e) {
				throw $e;
	        }
		}
	}
}